package com.itheima.health.service.impl;


import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.PageDate;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    @Transactional
    public void addAll(List<OrderSetting> orderSettings) {
        //批量添加
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate ();
            int number = orderSetting.getNumber ();
            editNumberByDate (orderDate, number);

        }
    }


    @Override
    @Transactional
    public void editNumberByDate(Date orderDate, int number) {
        //根据日期进行操作
        OrderSetting orderSetting = orderSettingDao.selectByOrderDate (orderDate);
        //不存在并加入
        if (orderSetting != null) {
            //存在并更新数据
            orderSettingDao.updateNumberById (orderSetting.getId (), number);
        } else {
            orderSettingDao.insert (new OrderSetting (orderDate, number));
        }
    }



//    @Override
//    public List<PageDate> getOrderSettingByMonth(Integer year, Integer month) throws ParseException {
//
//      List<Map< ArrayList,Object>> list = new ArrayList<>();
//
//
//        return null;
//    }

    @Override
    public Result getOrderSettingByMonth(Integer year, Integer month) throws ParseException {

        String fisrtDayOfMonth = getFisrtDayOfMonth(year, month);//第一天
        String lastDayOfMonth = getLastDayOfMonth(year,month );//最后一天
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
//        Date parse = simpleDateFormat.parse(fisrtDayOfMonth);
//        Date parse2 = simpleDateFormat.parse(lastDayOfMonth);
        List<PageDate> pageDateList = new ArrayList<>();


        List<OrderSetting> orderSettings = orderSettingDao.selectMonth(fisrtDayOfMonth, lastDayOfMonth);//根据年月获取数据数组
        log.info("orderSettings:"+orderSettings);
        for (int i = 0; i < orderSettings.size(); i++) {
            OrderSetting setting = orderSettings.get(i);
            Date orderDate = setting.getOrderDate();
            String format = simpleDateFormat.format(orderDate);
            int day = orderDate.getDate();


//            int month1 = orderDate.getMonth();
//            int day = orderDate.getDay();
            int number = setting.getNumber();
            pageDateList.add(new PageDate(day,number,setting.getReservations()));
        }
//      String string = pageDateList.toString();

        System.out.println(pageDateList);


        return new Result(true,"成功",pageDateList);
    }





    /**
     *   获取每个月的第一天
     * @param year
     * @param month
     * @return
     */
    public static String getFisrtDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    /**
     * 获取每个月的最后一天
     * @param year
     * @param month
     * @return
     */

    public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);

        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);

        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }



}
